package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.admin.api.entity.SysTenant;
import com.evaluation.kexingcp.admin.service.SysTenantService;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.common.security.annotation.Inner;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationTenantTypesEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationTenantTypesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户类型表
 *
 * @author pig
 * @date 2024-09-02 10:23:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationTenantTypes" )
@Tag(description = "evaluationTenantTypes" , name = "租户类型表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationTenantTypesController {

    private final EvaluationTenantTypesService evaluationTenantTypesService;
    private final SysTenantService sysTenantService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationTenantTypes 租户类型表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_view')" )
    public R getEvaluationTenantTypesPage(@ParameterObject Page page, @ParameterObject EvaluationTenantTypesEntity evaluationTenantTypes) {
        LambdaQueryWrapper<EvaluationTenantTypesEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(evaluationTenantTypes.getName()), EvaluationTenantTypesEntity::getName,evaluationTenantTypes.getName());
        return R.ok(evaluationTenantTypesService.page(page, wrapper));
    }


    @GetMapping("/list" )
    @Inner(value = false)
    public R list() {
        return R.ok(evaluationTenantTypesService.list());
    }


    /**
     * 通过id查询租户类型表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationTenantTypesService.getById(id));
    }

    /**
     * 新增租户类型表
     * @param evaluationTenantTypes 租户类型表
     * @return R
     */
    @Operation(summary = "新增租户类型表" , description = "新增租户类型表" )
    @SysLog("新增租户类型表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_add')" )
    public R save(@RequestBody EvaluationTenantTypesEntity evaluationTenantTypes) {
        return R.ok(evaluationTenantTypesService.save(evaluationTenantTypes));
    }

    /**
     * 修改租户类型表
     * @param evaluationTenantTypes 租户类型表
     * @return R
     */
    @Operation(summary = "修改租户类型表" , description = "修改租户类型表" )
    @SysLog("修改租户类型表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_edit')" )
    public R updateById(@RequestBody EvaluationTenantTypesEntity evaluationTenantTypes) {
        sysTenantService.list(Wrappers.lambdaQuery(SysTenant.class).eq(SysTenant::getOrgType, evaluationTenantTypes.getName()))
                .forEach(tenant -> {
                    tenant.setMenuId(evaluationTenantTypes.getMenus());
                    sysTenantService.updateTenant(tenant);
                });

        return R.ok(evaluationTenantTypesService.updateById(evaluationTenantTypes));
    }


    /**
     * 通过id删除租户类型表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除租户类型表" , description = "通过id删除租户类型表" )
    @SysLog("通过id删除租户类型表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationTenantTypesService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationTenantTypes 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTenantTypes_export')" )
    public List<EvaluationTenantTypesEntity> export(EvaluationTenantTypesEntity evaluationTenantTypes,Long[] ids) {
        return evaluationTenantTypesService.list(Wrappers.lambdaQuery(evaluationTenantTypes).in(ArrayUtil.isNotEmpty(ids), EvaluationTenantTypesEntity::getId, ids));
    }
}